
#include "System/Memory.h"
#include "ItemManager.h"
#include "game.h"
#include "GameObjectManager.h"

ItemManager::ItemManager()
{

	
}

void ItemManager::SaveData(CWriteFile* stream)
{
	stream->WriteS32(m_lastBall);

	int iCrtBall =-1;
	/*for(int i=0;i<6;i++)
		if(m_balloons[i] == m_GameplayCrtBallon)
			iCrtBall = i;*/
	stream->WriteS32(iCrtBall);
}
void ItemManager::LoadData(CReadFile* stream)
{
	m_lastBall = stream->ReadS32();

	int iCrtBall =  stream->ReadS32();
	/*if(iCrtBall == -1)
		m_GameplayCrtBallon = NULL;
	else
		m_GameplayCrtBallon = m_balloons[iCrtBall];*/
}

void ItemManager::initialize()
{
	Item* item;
	float padX,padY;
	padX = 0.5f;
	padY = -0.6f;
	m_items.reallocate(6);

	item = GL_NEW Item();
	item->Init(ITEM_BALL1,padX,padY);
	m_items.push_back(item);

	padX+= 0.7f;
	item = GL_NEW Item();
	item->Init(ITEM_BALL2,padX,padY);
	m_items.push_back(item);

	padX+= 0.7f;
	item = GL_NEW Item();
	item->Init(ITEM_BALL3,padX,padY);
	m_items.push_back(item);

	padX+= 0.7f;
	item = GL_NEW Item();
	item->Init(ITEM_BALL4,padX,padY);
	m_items.push_back(item);

	padX+= 0.7f;
	item = GL_NEW Item();
	item->Init(ITEM_BALL5,padX,padY);
	m_items.push_back(item);

	padX+= 0.7f;
	item = GL_NEW Item();
	item->Init(ITEM_BALL6,padX,padY);
	m_items.push_back(item);

	/*m_balloons[0] = GL_NEW BalloonRed();
	m_balloons[0]->setRefItem(m_items[0]);
	m_balloons[0]->SetEnabled(false);
	GetGameObjectManager()->GetGameObjects().AddObject(m_balloons[0]);

	m_balloons[1] = GL_NEW BalloonGreen();
	m_balloons[1]->setRefItem(m_items[1]);
	m_balloons[1]->SetEnabled(false);
	GetGameObjectManager()->GetGameObjects().AddObject(m_balloons[1]);

	m_balloons[2] = GL_NEW BalloonBlue();
	m_balloons[2]->setRefItem(m_items[2]);
	m_balloons[2]->SetEnabled(false);
	GetGameObjectManager()->GetGameObjects().AddObject(m_balloons[2]);

	m_balloons[3] = GL_NEW BalloonCyan();
	m_balloons[3]->setRefItem(m_items[3]);
	m_balloons[3]->SetEnabled(false);
	GetGameObjectManager()->GetGameObjects().AddObject(m_balloons[3]);

	m_balloons[4] = GL_NEW BalloonMagenta();
	m_balloons[4]->setRefItem(m_items[4]);
	m_balloons[4]->SetEnabled(false);
	GetGameObjectManager()->GetGameObjects().AddObject(m_balloons[4]);

	m_balloons[5] = GL_NEW BalloonYellow();
	m_balloons[5]->setRefItem(m_items[5]);
	m_balloons[5]->SetEnabled(false);
	GetGameObjectManager()->GetGameObjects().AddObject(m_balloons[5]);*/

	/*m_GameplayCrtBallon = m_balloons[0];
	m_balloons[0]->SetEnabled(true);
	m_lastBall = 0;*/
	m_GameplayCrtBallon = NULL;
	m_lastBall = -1;
}

void ItemManager::ThrowBall(float cx,float cy, float ex, float ey)
{
	if(m_GameplayCrtBallon)
	{
		//m_lastBall = m_GameplayCrtBallon->GetNature();
		m_GameplayCrtBallon->SetPath(cx,cy,ex,ey);
		switch (m_GameplayCrtBallon->GetNature())
		{
		case RED_NATURE:
			m_lastBall = 0;
			if(m_items[0]->DownPower() == 0)
			{
				m_items[4]->SetReady(false);
				m_items[5]->SetReady(false);
			}
			break;
		case GREEN_NATURE:
			m_lastBall = 1;
			if(m_items[1]->DownPower() == 0)
			{
				m_items[3]->SetReady(false);
				m_items[5]->SetReady(false);
			}
			break;
		case BLUE_NATURE:
			m_lastBall = 2;
			if(m_items[2]->DownPower() == 0)
			{
				m_items[3]->SetReady(false);
				m_items[4]->SetReady(false);
			}
			break;
		case CYAN_NATURE:
			m_lastBall = 3;
			m_items[1]->DownPower();
			m_items[2]->DownPower();
			m_items[3]->DownPower();
			break;
		case MAGENTA_NATURE:
			m_lastBall = 4;
			m_items[0]->DownPower();
			m_items[2]->DownPower();
			m_items[4]->DownPower();
			break;
		case YELLOW_NATURE:
			m_lastBall = 5;
			m_items[0]->DownPower();
			m_items[1]->DownPower();
			m_items[5]->DownPower();
			break;
		}

	}
}
void ItemManager::SetCrtBalloon(int iball)
{
	//ASSERT(iball >=0 && iball <6);
	if(iball < 0 || iball >=6)
	{
		m_GameplayCrtBallon = NULL;
		return;
	}

	if(m_items[iball]->GetPower() <= 0)
		return;

	int nature = 0;
	switch (iball)
	{
	case 0:
		nature = RED_NATURE;
		break;
	case 1:
		nature = GREEN_NATURE;
		break;
	case 2:
		nature = BLUE_NATURE;
		break;
	case 3:
		nature = CYAN_NATURE;
		break;
	case 4:
		nature = MAGENTA_NATURE;
		break;
	case 5:
		nature = YELLOW_NATURE;
		break;
	}
	/*if(m_GameplayCrtBallon == m_balloons[iball])
		return;*/
	/*if(m_balloons[iball]->IsEnabled())
		return;*/

	/*if(m_balloons[iball]->m_refItem->GetPower() <= 0)
		return;*/

	/*if(m_GameplayCrtBallon != NULL)
	{
		if(m_GameplayCrtBallon->m_state == -1)
			m_GameplayCrtBallon->SetEnabled(false);
	}*/
	if(m_GameplayCrtBallon == NULL)
	{
		m_GameplayCrtBallon = (Balloon*)GetGameObjectManager()->Spawn("Balloon");		
		m_GameplayCrtBallon->SetEnabled(true);
	}
	m_GameplayCrtBallon->SetNature(nature);
	//m_GameplayCrtBallon = m_balloons[iball];
	
	//m_GameplayCrtBallon->m_state = BALL_STATE_BACK;
}

void ItemManager::UpdateAll()
{
	if(m_GameplayCrtBallon == NULL && m_lastBall != -1)
	{		
		SetCrtBalloon(m_lastBall);		
	}
	for(int i =0; i<m_items.size();i++)
	{
		m_items[i]->Update();
	}
}

void ItemManager::DrawAll()
{
	for(int i =0; i<m_items.size();i++)
	{
		m_items[i]->Draw();
	}
}